<?php
require_once "lib/GRS.php";

$grs = new GRS();
	
$grs->mergeParams($_REQUEST['_grs_post_data_']);

$action = $grs->params('action');

switch($action){
	
	case 'ADD_GAME':
		$game_name = $grs->params('game_name');
		$game_img = $grs->params('game_img');
        if(!$game_name){
            error("Escriba el nombre de un juego.");
        }
		//revisar si el juego existe
		if(@mysql_num_rows(dbquery("SELECT `id` FROM `grs_games` WHERE `name`='{$game_name}'")) == 0){
			if(dbquery("INSERT INTO `grs_games` (`name`, `image`) VALUES ('{$game_name}', '{$game_img}')")){
				msg("Juego agregado correctamente");
			}else{
				//error(mysql_error());	
				exit;
			}			
		}else{
			error("El juego ya existe");	
		}
					
	break;
	
	case 'ADD_MODE':
        $mode_name = $grs->params('mode_name');
        if(!$mode_name){
            error("Escriba el nombre de del modo de juego que desea agregar.");
        }
		//revisar si el modo existe
		if(@mysql_num_rows(dbquery("SELECT `id` FROM `grs_modes` WHERE `name`='{$mode_name}'")) == 0){
			if(dbquery("INSERT INTO `grs_games` (`name`) VALUES ('{$mode_game}')")){
				msg("Modo agregado correctamente");
			}else{
				//error(mysql_error());	
				exit;
			}			
		}else{
			error("El modo ya existe");	
		}
	break;
	
	case 'ADD_TRACK':
        $track_name = $grs->params('track_name');
        if(!$mode_name){
            error("Escriba el nombre de de la pista de juego que desea agregar.");
        }
		//revisar si el modo existe
		if(@mysql_num_rows(dbquery("SELECT `id` FROM `grs_tracks` WHERE `name`='{$track_name}'")) == 0){
			if(dbquery("INSERT INTO `grs_games` (`name`) VALUES ('{$track_name}')")){
				msg("Pista agregada correctamente");
			}else{
				//error(mysql_error());	
				exit;
			}			
		}else{
			error("La pista ya existe");	
		}
	break;
	
	case 'ADD_PLAYER':
        $player_name = $grs->params('player_name');
		$player_email = $grs->params('player_email');
        if(!$player_name){
            error("Escriba el nombre del jugador.");
        }
		//revisar si el jugador existe
		if(@mysql_num_rows(dbquery("SELECT `id` FROM `grs_players` WHERE `name`='{$player_name}'")) == 0){
			if(dbquery("INSERT INTO `grs_players` (`name`, `email`) VALUES ('{$player_name}', '{$player_email}')")){
				msg("Jugador agregado correctamente");
			}else{
				//error(mysql_error());	
				exit;
			}			
		}else{
			error("El jugador ya existe");	
		}
			
	break;
	
	case 'ADD_REC':
        $game_id = $grs->params("game_id");
        $mode_id = $grs->params("mode_id");
        $track_id = $grs->params("track_id");  
        $player_id = $grs->getPlayerByName($grs->params("player"));
        $car_id = $grs->params("car_id"); 
        $time_hour = $grs->params("time_hour");
        $time_minute = $grs->params("time_minute");
        $time_second = $grs->params("time_second");
        
        if(!$player_id){error("Jugador no encontrado.");}        
        if(!$car_id){error("No ha seleccionado un coche."); }
        
        if(!$time_hour){$time_hour = "00";}
        if(!$time_minute){$time_minute = "00";}
        if(!$time_second){$time_second = "00";}
        
        $time = mktime($time_hour, $time_minute, $time_second);
        
        if($time)
            $time = strftime("%H:%M:%S", $time);
        else
            error("Tiempo introducido incorrecto.");
        
        $sql = "INSERT INTO `grs_records` (player_id, car_id, track_id, mode_id, game_id, time)";
        $sql .= "VALUES ('{$player_id}', '{$car_id}', '{$track_id}', '{$mode_id}', '{$game_id}', '{$time}')";
        
        if(dbquery($sql)){
				msg("Juego agregado correctamente");
			}else{
                exit;
				//error(mysql_error());	
			}
	break;
}

function msg($str){
	echo "<span style='color:#0F0'>".$str."</span>";
    exit;
}

function error($str){
	echo "<span style='color:#F00'>".$str."</span>";
	exit;	
}
	

?>